package com.example.hyw.szysteam.presenter;

import android.util.Log;

import com.example.hyw.szysteam.POJO.HttpResult;
import com.example.hyw.szysteam.POJO.Practice;
import com.example.hyw.szysteam.POJO.Review;
import com.example.hyw.szysteam.model.HomeWorkModel;
import com.example.hyw.szysteam.model.PracticeModel;
import com.example.hyw.szysteam.model.ScoreModel;
import com.example.hyw.szysteam.view.PracticeActivity;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by HYW on 2018/6/12.
 */

public class PracticePresenter {
    private PracticeModel practiceModel=new PracticeModel();
    private ScoreModel scoreModel=new ScoreModel();
    private PracticeActivity practiceActivity;

    public PracticePresenter(PracticeActivity practiceActivity) {
        this.practiceActivity = practiceActivity;
    }

    public void getPractice(Integer testId){
        practiceModel.getPractice(testId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpResult<ArrayList<Practice>>>() {
                    @Override
                    public void accept(HttpResult<ArrayList<Practice>> arrayListHttpResult) throws Exception {
                        practiceActivity.initPractice(arrayListHttpResult.getContent());
                    }
                });
    }

    public void initPractice(int number,int operator,int range){
        practiceModel.initPractice(number,operator,range)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ArrayList<Practice>>() {
                    @Override
                    public void accept(ArrayList<Practice> practices) throws Exception {
                        practiceActivity.initPractice(practices);
                    }
                });
    }



    public void correct(final ArrayList<Practice> practices, final ArrayList<String> answer){
        practiceModel.correct(practices,answer)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        ArrayList<Review> reviews=new ArrayList<>();
                        for (int i=0;i<practices.size();i++){
                            Review r=new Review();
                            r.setQuestion(practices.get(i).getQuestion());
                            r.setAnswer(practices.get(i).getAnswer());
                            r.setMyAnswer(answer.get(i));
                            r.setCollect(0);
                            r.setId(practices.get(i).getId());
                            reviews.add(r);
                        }
                        practiceActivity.showScore(integer,reviews);
                    }
                });
    }

    public void setScore(String email,int testid,int score){
        scoreModel.setScore(email, testid, score)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpResult<Integer>>() {
                    @Override
                    public void accept(HttpResult<Integer> integerHttpResult) throws Exception {
                        return;
                    }
                });
    }

    public void getScore(String email,int testid){
        scoreModel.getScore(email, testid)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpResult<Integer>>() {
                    @Override
                    public void accept(HttpResult<Integer> integerHttpResult) throws Exception {
                        if (integerHttpResult.getResult()==0){
                            return;
                        }else {
                            practiceActivity.completed(integerHttpResult.getContent());
                        }
                    }
                });
    }
}
